package com.doit.day02;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;

public class HeroIterator implements Iterator<Hero> {
    String line = null;
    BufferedReader br = null;


    public HeroIterator() throws Exception {
        br = new BufferedReader(new FileReader("data/Hero.txt"));
    }

    //1.打开文件
    //一行一行的读取数据
    @Override
    public boolean hasNext() {
        try {
            line = br.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return line != null;
    }

    @Override
    public Hero next() {
        String[] arr = line.split(",");
        int id = Integer.parseInt(arr[0]);
        String[] arr1 = arr[1].split("_");
        String name = arr1[0];
        String[] arr2 = arr1[1].split("::");
        int power = Integer.parseInt(arr2[0]);
        int faceValue = Integer.parseInt(arr2[1]);
        return new Hero(id,name,power,faceValue);
    }
}
